package com.oba.king.businessprocess.storagemanage.outstorage;
/**
 * Title:           OutStorageBean
 * Description:
 * Copyright:       Copyright (c) 2010
 * Company:         King Top Limited
 * @author          Wu Liang
 * @version 1.0
 *
 * MODIFICATION HISTORY
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * Date Modified        Modified By         Comments
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 24/05/2010           Wu Liang            Code Creation
 */
import java.util.List;

import com.oba.base.dao.BaseDao;
import com.oba.base.interfaces.IBaseBo;
import com.oba.base.interfaces.IHsqlBuilder;
import com.oba.base.util.Util;
import com.oba.king.businessprocess.storagemanage.instorage.InStorage;
import com.oba.king.businessprocess.storagemanage.instorage.InStorageCondition;
import com.oba.king.datalib.productinfo.ProductInfo;
import com.oba.king.sysmanager.Dept;

public class OutStorageDao extends BaseDao {
	private final static String deleteSqlStr = "DELETE FROM OUTSTORAGEPRDT WHERE OUT_STORAGE_ID = ";
	public IHsqlBuilder buildAll(){
		IHsqlBuilder sql = this.getSqlBuilder("from OutStorage outSto");
		sql.addend("order by outSto.outStorageCode");
		return sql;
	}
	/**
	 * 初始化
	 * @param outStorage
	 * @return
	 */
	public List<OutStoragePrdt> findOutStoragePrdtByOutStoId(OutStorage outStorage) {
		IHsqlBuilder sql = getSqlBuilder("from OutStoragePrdt osp");
		sql.add("osp.outStorage = ?" ,outStorage);
		return find(sql);
	} 
	
	/**
	 * 查询需要审核的出库单进行审核
	 * @return
	 */
	public IHsqlBuilder findNeedAuditOutStorage(){
		IHsqlBuilder sql = getSqlBuilder("from OutStorage outSto");
		sql.add("outSto.auditStatus in (1,4)");//1 新增未审核 4修改未审核
		sql.addend("order by outSto.outStorageCode");
		return sql;
	}
	
	/**
	 * 显示详细
	 */
	public IHsqlBuilder srchByOutStorageCode(OutStorage outStorage){
		IHsqlBuilder sql = this.getSqlBuilder("from OutStoragePrdt osp");
		sql.add("osp.outStorage = ?",outStorage);
		return sql;
	}
	
	/**
	 * 通过入库单信息查询入库中间表中的药品信息
	 */
	public List<OutStoragePrdt> srchOutStoragePrdt(OutStorage outStorage){
		IHsqlBuilder sql = getSqlBuilder("from OutStoragePrdt osp");
		sql.add("osp.outStorage.outStorageId = ?", outStorage.getOutStorageId());
		return find(sql);
	}
	
	/**
	 * 查询部门，当新增入库单中的信息时需要增加部门信息
	 * @return 返回部门信息
	 */
	public List<Dept> buildAllDept() {
		return find("from Dept dept");
	}
	
	/**
	 * 删除出库单：1先删除中间表中的，再删除入库表中信息
	 */
	public void deleteOutStorePrdt(OutStorage is){
		jdao.execute(deleteSqlStr+is.getOutStorageId());
		delete(is);
	}
	
	/**
	 * 将所有药品信息查询出来
	 * TODO 可以将一个出库单中的产品排除
	 * @return
	 */
	public IHsqlBuilder srchPrdtForChoose(List<Long> list){
		String idsStr ="";
		if(list.size()!=0){
			idsStr = Util.idsFormat(list);			
		}
		IHsqlBuilder sql = this.getSqlBuilder("from ProductInfo prdt");
		if(idsStr!=null && !idsStr.trim().equals("")){
			sql.add("prdt.productId not in "+idsStr);
		}		
		sql.addend("order by prdt.productName");
		return sql;
	}
	
	/**
	 * 入库单的模糊查询
	 * @param inStorageCondition 查询条件
	 * @return
	 */
	public IHsqlBuilder srchByOutStorageCondition(OutStorageCondition outStorageCondition){
		IHsqlBuilder sql = getSqlBuilder("from OutStorage outSto");
		if(outStorageCondition.getOutStorageCode()!=null&& !outStorageCondition.getOutStorageCode().trim().equals("")){
			sql.add("outSto.outStorageCode =?", outStorageCondition.getOutStorageCode().toString().trim());			
		} 
		if(outStorageCondition.getResponMan()!=null && !outStorageCondition.getResponMan().trim().equals("")){
			sql.add("outSto.responMan like ?", outStorageCondition.getResponMan().trim(),"%","%");			
		}
		if(outStorageCondition.getOutDate()!=null && !outStorageCondition.getOutDate().equals("")){
			sql.add("outSto.outDate > ?", outStorageCondition.getOutDate());			
		}
		if(outStorageCondition.getEndDate()!=null && !outStorageCondition.getEndDate().equals("")){
			sql.add("outSto.outDate < ?", outStorageCondition.getEndDate());			
		}
		if(outStorageCondition.getAuditStatus()!=null && !outStorageCondition.getAuditStatus().equals("")&&!"0".equals(outStorageCondition.getAuditStatus())){
			sql.add("outSto.auditStatus = ?", outStorageCondition.getAuditStatus());			
		}
		sql.addend("order by outSto.outStorageCode");
		return sql;
	}
	
	/**
	 * 查询指定药品增加到入库单中
	 * @param productSerialId 药品编号
	 * @param productName 药品名称
	 * @return
	 */
	public IHsqlBuilder srchPrdtForChoose(List<Long> productIdList, OutStorageCondition isc){
		String idsStr = Util.idsFormat(productIdList);	
		IHsqlBuilder sql = this.getSqlBuilder("from ProductInfo prdt");
		sql.add("prdt.productSerialId = ?",isc.getProductSerialId());
		sql.add("prdt.productName like ?",isc.getProductName(),"%","%");		
		sql.add("prdt.productId not in "+idsStr);
		sql.addend("order by prdt.productName");
		return sql;
	}
	

}
